use sea_orm::entity::prelude::*;
use salvo::prelude::*;
use serde::{Deserialize, Serialize};
use chrono::NaiveDateTime;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "role")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    pub tenant_id: Option<i64>,
    pub create_time: NaiveDateTime,
    pub create_by: String,
    pub update_time: NaiveDateTime,
    pub update_by: String,
    pub deleted: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(has_many = "super::user_role::Entity")]
    UserRole,
}

impl Related<super::user_role::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::UserRole.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Serialize, Deserialize, Extractible, Debug)]
#[salvo(extract(default_source(from = "body", parse = "json")))]
pub struct Role {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    pub tenant_id: Option<i64>,
    pub create_time: NaiveDateTime,
    pub create_by: String,
    pub update_time: NaiveDateTime,
    pub update_by: String,
}

#[derive(Serialize, Deserialize, Debug, ToSchema)]
pub struct SafeRole {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    pub tenant_id: Option<i64>,
}
